import { FindOptionsWhere, Like } from 'typeorm';
import { {{pascalCase name}} } from './models/{{kebabCase name}}.entity';
import {
  COURSE_CREATE_FAIL,
  COURSE_DEL_FAIL,
  COURSE_NOT_EXIST,
  COURSE_UPDATE_FAIL,
} from './../../common/constants/code';
import { Result } from '@/common/dto/result.type';
import { Args, Mutation, Resolver, Query } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '@/common/guards/auth.guard';
import { SUCCESS } from '@/common/constants/code';
import { {{pascalCase name}}Result, {{pascalCase name}}Results } from './dto/result-{{kebabCase name}}.output';
import { {{pascalCase name}}Input } from './dto/{{kebabCase name}}.input';
import { {{pascalCase name}}Type } from './dto/{{kebabCase name}}.type';
import { {{pascalCase name}}Service } from './{{kebabCase name}}.service';
import { CurUserId } from '@/common/decorators/current-user.decorator';
import { PageInput } from '@/common/dto/page.input';

@Resolver(() => {{pascalCase name}}Type)
@UseGuards(GqlAuthGuard)
export class {{pascalCase name}}Resolver {
  constructor(private readonly {{kebabCase name}}Service: {{pascalCase name}}Service) {}

  @Query(() => {{pascalCase name}}Result)
  async get{{pascalCase name}}Info(@Args('id') id: string): Promise<{{pascalCase name}}Result> {
    const result = await this.{{kebabCase name}}Service.findById(id);
    if (result) {
      return {
        code: SUCCESS,
        data: result,
        message: '获取成功',
      };
    }
    return {
      code: COURSE_NOT_EXIST,
      message: '课程信息不存在',
    };
  }

  @Mutation(() => {{pascalCase name}}Result)
  async commit{{pascalCase name}}Info(
    @Args('params') params: {{pascalCase name}}Input,
    @CurUserId() userId: string,
    @CurOrgId() orgId: string,
    @Args('id', { nullable: true }) id: string,
  ): Promise<Result> {
    if (!id) {
      const res = await this.{{kebabCase name}}Service.create({
        ...params,
        createdBy: userId,
        org: {
          id: orgId,
        }
      });
      if (res) {
        return {
          code: SUCCESS,
          message: '创建成功',
        };
      }
      return {
        code: COURSE_CREATE_FAIL,
        message: '创建失败',
      };
    }
    const {{kebabCase name}} = await this.{{kebabCase name}}Service.findById(id);
    if ({{kebabCase name}}) {
      const res = await this.{{kebabCase name}}Service.updateById({{kebabCase name}}.id, {
        ...params,
        updatedBy: userId,
      });
      if (res) {
        return {
          code: SUCCESS,
          message: '更新成功',
        };
      }
      return {
        code: COURSE_UPDATE_FAIL,
        message: '更新失败',
      };
    }
    return {
      code: COURSE_NOT_EXIST,
      message: '课程信息不存在',
    };
  }

  @Query(() => {{pascalCase name}}Results)
  async get{{pascalCase name}}s(
    @Args('page') page: PageInput,
    @CurUserId() userId: string,
    @Args('name', { nullable: true }) name?: string,
  ): Promise<{{pascalCase name}}Results> {
    const { pageNum, pageSize } = page;
    const where: FindOptionsWhere<{{pascalCase name}}> = { createdBy: userId };
    if (name) {
      where.name = Like(`%${name}%`);
    }
    const [results, total] = await this.{{kebabCase name}}Service.find{{pascalCase name}}s({
      start: (pageNum - 1) * pageSize,
      length: pageSize,
      where,
    });
    return {
      code: SUCCESS,
      data: results,
      page: {
        pageNum,
        pageSize,
        total,
      },
      message: '获取成功',
    };
  }

  @Mutation(() => Result)
  async delete{{pascalCase name}}(
    @Args('id') id: string,
    @CurUserId() userId: string,
  ): Promise<Result> {
    const result = await this.{{kebabCase name}}Service.findById(id);
    if (result) {
      const delRes = await this.{{kebabCase name}}Service.deleteById(id, userId);
      if (delRes) {
        return {
          code: SUCCESS,
          message: '删除成功',
        };
      }
      return {
        code: COURSE_DEL_FAIL,
        message: '删除失败',
      };
    }
    return {
      code: COURSE_NOT_EXIST,
      message: '门店信息不存在',
    };
  }
}
